/****************************************Copyright (c)****************************************************
**                               China Mobile M2M Co., LTD
**
**                                 http://iot.10086.cn/
**
**--------------File Info---------------------------------------------------------------------------------
** File name:           api_i2c.c
** Last modified Date:  20160216
** Last Version:        V1.0
** Descriptions:        i2c driver
**
**--------------------------------------------------------------------------------------------------------
** Created by:          yangpengbo                  ID:03900265
** Created date:        20160216
** Version:             V1.00
** Descriptions:        none
**
**--------------------------------------------------------------------------------------------------------
** Modified by:         x
** Modified date:       x
** Version:             x
** Descriptions:        x
**
** Rechecked by:
*********************************************************************************************************/
#include "include.h"

/*********************************************************************************************************
**                                          GLOBAL
*********************************************************************************************************/

/*********************************************************************************************************
**                                          FUNCTIONS
*********************************************************************************************************/
/*********************************************************************************************************
** Function name:       I2C_Init
** Descriptions:        i2c init
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void I2C_Init(void)
{
    iot_gpioModeConfigure(I2C_SCL_IO,IOT_GPIO_MODE_GPIO);
    iot_gpioDirectionConfigure(I2C_SCL_IO,IOT_GPIO_DIR_OUTPUT);
    iot_gpioPullEnable(I2C_SCL_IO, 0);
    iot_gpioModeConfigure(I2C_SDA_IO,IOT_GPIO_MODE_GPIO);
    iot_gpioDirectionConfigure(I2C_SDA_IO,IOT_GPIO_DIR_OUTPUT);
    iot_gpioPullEnable(I2C_SDA_IO, 0);
}

/*********************************************************************************************************
** Function name:       I2C_Start
** Descriptions:        I2C_Start
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void I2C_Start(void)
{
    iot_gpioWrite(I2C_SDA_IO,1);
    iot_delayUs(1);
    iot_gpioWrite(I2C_SCL_IO,1);
    iot_delayUs(5);
    iot_gpioWrite(I2C_SDA_IO,0);
    iot_delayUs(4);
    iot_gpioWrite(I2C_SCL_IO,0);
    iot_delayUs(1);
}

/*********************************************************************************************************
** Function name:       I2C_Stop
** Descriptions:        I2C_Stop
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void I2C_Stop(void)
{
    iot_gpioWrite(I2C_SDA_IO,0);
    iot_delayUs(1);
    iot_gpioWrite(I2C_SCL_IO,1);
    iot_delayUs(4);
    iot_gpioWrite(I2C_SDA_IO,1);
    iot_delayUs(3);
}

/*********************************************************************************************************
** Function name:       I2C_Ack
** Descriptions:        I2C_Ack
** input parameters:    ackflag
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
void I2C_Ack(BOOL ackflag)
{
    if (ackflag)
    {
        iot_gpioWrite(I2C_SDA_IO,1);
    }
    else
    {
        iot_gpioWrite(I2C_SDA_IO,0);
    }
    iot_delayUs(1);
    iot_gpioWrite(I2C_SCL_IO,1);
    iot_delayUs(4);
    iot_gpioWrite(I2C_SCL_IO,0);
}

/*********************************************************************************************************
** Function name:       I2C_WriteByte
** Descriptions:        I2C_WriteByte
** input parameters:    wdata
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
BOOL I2C_WriteByte(U8 wdata)
{
    U8 bitcnt;
    BOOL ret;
    U8 temp;

    bitcnt = 0;
    temp = wdata;
    while(1)
    {
        if (temp & 0x80)
        {
            iot_gpioWrite(I2C_SDA_IO,1);
        }
        else
        {
            iot_gpioWrite(I2C_SDA_IO,0);
        }
        bitcnt++;
        iot_gpioWrite(I2C_SCL_IO,1);
        iot_delayUs(1);
        if (bitcnt >= 8)
        {
            iot_gpioWrite(I2C_SCL_IO,0);
            iot_delayUs(1);
            break;
        }
        temp = wdata << bitcnt;
        iot_gpioWrite(I2C_SCL_IO,0);
        iot_delayUs(1);
    }
    iot_gpioDirectionConfigure(I2C_SDA_IO,IOT_GPIO_DIR_INPUT);
    iot_delayUs(1);
    iot_gpioWrite(I2C_SCL_IO,1);
    iot_delayUs(1);

    if (iot_gpioRead(I2C_SDA_IO))
    {
        ret = 0;
    }
    else
    {
        ret = 1;
    }
    iot_gpioWrite(I2C_SCL_IO,0);
    iot_gpioDirectionConfigure(I2C_SDA_IO,1);

    return ret;
}

/*********************************************************************************************************
** Function name:       I2C_ReadByte
** Descriptions:        I2C_ReadByte
** input parameters:    none
** output parameters:   none
** Returned value:      none
*********************************************************************************************************/
U8 I2C_ReadByte(void)
{
    U8 bitcnt;
    U8 rdata;

    rdata = 0;
    iot_gpioDirectionConfigure(I2C_SDA_IO,0);
    iot_delayUs(1);

    iot_gpioWrite(I2C_SCL_IO,0);
    for (bitcnt = 0; bitcnt < 8; bitcnt++)
    {
        iot_delayUs(1);
        iot_gpioWrite(I2C_SCL_IO,1);
        iot_delayUs(1);
        rdata = rdata << 1;
        if (iot_gpioRead(I2C_SDA_IO))
        {
            rdata += 1;
        }
        iot_gpioWrite(I2C_SCL_IO,0);
    }
    iot_gpioDirectionConfigure(I2C_SDA_IO,1);

    return rdata;
}

/*********************************************************************************************************
End Of File
*********************************************************************************************************/


